/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.FunctionsDAO;
import dao.RoleDAO;
import dao.RoleFunctionsDAO;
import dto.Functions;
import dto.Role;
import dto.RoleFunctions;
import java.util.ArrayList;
import javax.swing.JFrame;

/**
 *
 * @author Administrator
 */
public class RoleFunctionForm extends javax.swing.JPanel {

    private ArrayList<Role> roles;
    private RoleDAO daoR;
    private ArrayList<Functions> functions;
    private FunctionsDAO daoF;
    private RoleFunctionsDAO daoRF;

    /**
     * Creates new form RoleFunctionForm
     */
    public RoleFunctionForm() {
        initComponents();
        daoR = new RoleDAO();
        daoF = new FunctionsDAO();
        daoRF = new RoleFunctionsDAO();
        initDatas();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        lblRole = new javax.swing.JLabel();
        ScrollPane1 = new javax.swing.JScrollPane();
        listRole = new javax.swing.JList();
        lblFunctions = new javax.swing.JLabel();
        ScrollPane2 = new javax.swing.JScrollPane();
        tblFunctions = new javax.swing.JTable();

        setLayout(new java.awt.GridBagLayout());

        lblRole.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblRole.setText("Role");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 114;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 0);
        add(lblRole, gridBagConstraints);

        listRole.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listRole.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listRoleValueChanged(evt);
            }
        });
        ScrollPane1.setViewportView(listRole);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 112;
        gridBagConstraints.ipady = 252;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 11, 0);
        add(ScrollPane1, gridBagConstraints);

        lblFunctions.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFunctions.setText("Functions");
        lblFunctions.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 329;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 18, 0, 10);
        add(lblFunctions, gridBagConstraints);

        tblFunctions.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Name", "Description", "Selected"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblFunctions.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblFunctionsMouseClicked(evt);
            }
        });
        ScrollPane2.setViewportView(tblFunctions);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 352;
        gridBagConstraints.ipady = 248;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 11, 10);
        add(ScrollPane2, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void listRoleValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listRoleValueChanged
        // TODO add your handling code here:
        Role rSelected = (Role) listRole.getModel().getElementAt(listRole.getAnchorSelectionIndex());
        initFunctions(rSelected.getId());
    }//GEN-LAST:event_listRoleValueChanged

    private void tblFunctionsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFunctionsMouseClicked
        // TODO add your handling code here:
        if (listRole.getSelectedIndex() != -1) {
            Role rSelected = (Role) listRole.getModel().getElementAt(listRole.getAnchorSelectionIndex());
            int row = tblFunctions.rowAtPoint(evt.getPoint());
            Functions fSelected = (Functions) tblFunctions.getValueAt(row, 0);
            boolean checked = (boolean) tblFunctions.getValueAt(row, 2);
            RoleFunctions p = new RoleFunctions(rSelected.getId(), fSelected.getId());
            if (checked) {
                daoRF.create(p);
                System.out.println(daoRF.getLastError());
            } else {
                daoRF.delete(p);
                System.out.println(daoRF.getLastError());
            }
        }
    }//GEN-LAST:event_tblFunctionsMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane ScrollPane1;
    private javax.swing.JScrollPane ScrollPane2;
    private javax.swing.JLabel lblFunctions;
    private javax.swing.JLabel lblRole;
    private javax.swing.JList listRole;
    private javax.swing.JTable tblFunctions;
    // End of variables declaration//GEN-END:variables

    private void initDatas() {
        initRoles();
        initFunctions(0);
    }

    public void initFunctions(int roleId) {
        functions = daoF.ReadAll();
        Object[][] rowDatas = new Object[functions.size()][3];
        int i = 0;
        for (Functions f : functions) {
            rowDatas[i][0] = f;
            rowDatas[i][1] = f.getDescript();
            RoleFunctions rf = new RoleFunctions();
            rf.setRoleId(roleId);
            rf.setFunctionId(f.getId());
            rowDatas[i][2] = daoRF.isExist(rf);
            i++;
        }
        String[] columnNames = {
            "Name", "Description", "Selected"
        };
        tblFunctions.setModel(new javax.swing.table.DefaultTableModel(
                rowDatas,
                columnNames) {
            Class[] types = new Class[]{
                java.lang.Object.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, true
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
    }

    public void initRoles() {
        roles = daoR.ReadAll();
        listRole.setModel(new javax.swing.AbstractListModel() {
            Role[] rolesArray = roles.toArray(new Role[roles.size()]);

            @Override
            public int getSize() {
                return rolesArray.length;
            }

            @Override
            public Object getElementAt(int i) {
                return rolesArray[i];
            }
        });
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.getContentPane().add(new RoleFunctionForm());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}
